/**
 * 
 */
package com.vote.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2986798982667323994L;
	protected long timeMillis = System.currentTimeMillis();

	protected HttpServletRequest request = ServletActionContext.getRequest();

	public String readCookie(String cookKey) {
		String cookStr = "";
		Cookie cookies[] = request.getCookies();
		try {
			if (cookies != null && cookies.length > 0) {
				for (int i = 0; i < cookies.length; i++) {
					Cookie cook = cookies[i];
					if (cook != null) {
						if (cook.getName().equals(cookKey)) {
							cookStr = new String(cook.getValue().getBytes("UTF-8"), "UTF-8");
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			e.getMessage();
		}
		return cookStr;
	}
	protected String getRealIp() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = null;
		/*
		ip = request.getHeader("X-Real-IP");
		if (!StringUtils.hasText(ip)) {
			ip = request.getRemoteAddr();
		}
		*/		
		ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getRemoteAddr();
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("http_client_ip");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		// 如果是多级代理，那么取第一个ip为客户ip
		if(ip != null && ip.indexOf(",") != -1){
			ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
		}
		return ip;
	}

	protected void saveCooke(String key, String val) {
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			// Cookie cookie=new Cookie(key, new
			// String(Base64.encode(val.getBytes("UTF-8")),"UTF-8"));
			Cookie cookie = new Cookie(key, val);
			cookie.setMaxAge(Integer.MAX_VALUE);
			cookie.setPath("/");
			response.addCookie(cookie);
		} catch (Exception e) {
		}
	}
	
	protected String getReferer(){
		String lastUrl=request.getHeader("REFERER");
		if(StringUtils.isNotEmpty(lastUrl)){
			lastUrl=lastUrl.toLowerCase();
//			lastUrl=lastUrl.replaceAll("http://", "");
//			lastUrl=lastUrl.substring(0, lastUrl.indexOf("/"));
		}
		return lastUrl; 
	}
	
	public long getTimeMillis() {
		return timeMillis;
	}

	public void setTimeMillis(long timeMillis) {
		this.timeMillis = timeMillis;
	}
}
